package dto

import (
	"gorm.io/gorm"
	"project2/g"
	"project2/model"
	"project2/utils"
	"time"
)

type SysAccountDto struct {
	Id            uint      `json:"id"`
	UserName      string    `json:"userName"`
	PassWord      string    `json:"passWord"`
	LoginNum      uint      `json:"loginNum"`
	State         uint      `json:"state"`
	OnlieState    uint      `json:"onlieState"`
	IsAdmin       uint      `json:"isAdmin"`
	IpState       uint      `json:"ipState"`
	Ip            string    `json:"ip"`
	LastLoginTime time.Time `json:"lastLoginTime"`
	LastIp        string    `json:"lastIp"`
	TenantType    uint      `json:"tenantType"`
	TenantId      uint      `json:"tenantId"`
	DelState      uint      `json:"delState"`
	AddUser       string    `json:"addUser"`
	AddUserId     uint      `json:"addUserId"`
	AddTime       time.Time `json:"addTime"`
	EditUser      string    `json:"editUser"`
	EditUserId    uint      `json:"editUserId"`
	LastEditTime  time.Time `json:"lastEditTime"`
	ServerNumber  uint      `json:"serverNumber"`
	DelTime       time.Time `json:"delTime"`
}

func BuildSysAccountDto(_body *map[string]interface{}, dto *SysAccountDto) {
	body := *_body

	if body == nil {
		return
	}

	if body["id"] != nil && utils.ParserInt8(body["id"]) > 0 {
		dto.Id = uint(utils.ParserInt8(body["id"]))

	}

	if body["userName"] != nil && body["userName"].(string) != "" {
		dto.UserName = body["userName"].(string)
	}

	if body["passWord"] != nil && body["passWord"].(string) != "" {
		dto.PassWord = body["passWord"].(string)
	}

	if body["loginNum"] != nil && utils.ParserInt8(body["loginNum"]) > 0 {
		dto.LoginNum = uint(utils.ParserInt8(body["loginNum"]))
	}

	if body["state"] != nil && utils.ParserInt8(body["state"]) > 0 {
		dto.State = uint(utils.ParserInt8(body["state"]))
	}

	if body["onlieState"] != nil && utils.ParserInt8(body["onlieState"]) > 0 {
		dto.OnlieState = uint(utils.ParserInt8(body["onlieState"]))
	}

	if body["isAdmin"] != nil && utils.ParserInt8(body["isAdmin"]) > 0 {
		dto.IsAdmin = uint(utils.ParserInt8(body["isAdmin"]))
	}

	if body["ipState"] != nil && utils.ParserInt8(body["ipState"]) > 0 {
		dto.IpState = uint(utils.ParserInt8(body["ipState"]))
	}

	if body["ip"] != nil && body["ip"].(string) != "" {
		dto.Ip = body["ip"].(string)
	}

	if body["lastLoginTime"] != nil && !utils.Str2date(body["lastLoginTime"].(string)).IsZero() {
		dto.LastLoginTime = utils.Str2date(body["lastLoginTime"].(string))
	}

	if body["lastIp"] != nil && body["lastIp"].(string) != "" {
		dto.LastIp = body["lastIp"].(string)
	}

	if body["tenantType"] != nil && utils.ParserInt8(body["tenantType"]) > 0 {
		dto.TenantType = uint(utils.ParserInt8(body["tenantType"]))
	}

	if body["tenantId"] != nil && utils.ParserInt8(body["tenantId"]) > 0 {
		dto.TenantId = uint(utils.ParserInt8(body["tenantId"]))
	}

	if body["delState"] != nil && utils.ParserInt8(body["delState"]) > 0 {
		dto.DelState = uint(utils.ParserInt8(body["delState"]))
	}

	if body["addUser"] != nil && body["addUser"].(string) != "" {
		dto.AddUser = body["addUser"].(string)
	}

	if body["addUserId"] != nil && utils.ParserInt8(body["addUserId"]) > 0 {
		dto.AddUserId = uint(utils.ParserInt8(body["addUserId"]))
	}

	if body["addTime"] != nil && !utils.Str2date(body["addTime"].(string)).IsZero() {
		dto.AddTime = utils.Str2date(body["addTime"].(string))
	}

	if body["editUser"] != nil && body["editUser"].(string) != "" {
		dto.EditUser = body["editUser"].(string)
	}

	if body["editUserId"] != nil && utils.ParserInt8(body["editUserId"]) > 0 {
		dto.EditUserId = uint(utils.ParserInt8(body["editUserId"]))
	}

	if body["lastEditTime"] != nil && !utils.Str2date(body["lastEditTime"].(string)).IsZero() {
		dto.LastEditTime = utils.Str2date(body["lastEditTime"].(string))
	}

	if body["serverNumber"] != nil && utils.ParserInt8(body["serverNumber"]) > 0 {
		dto.ServerNumber = uint(utils.ParserInt8(body["serverNumber"]))
	}

	if body["delTime"] != nil && !utils.Str2date(body["delTime"].(string)).IsZero() {
		dto.DelTime = utils.Str2date(body["delTime"].(string))
	}

}

func (self *SysAccountDto) BuildQuery(query **gorm.DB) {
	(*query) = g.Db.Model(&model.SysAccount{})
	(*query).Where("del_time is not null")

	if self.Id > 0 {
		(*query).Where("id =?", self.Id)
	}

	if self.UserName != "" {
		(*query).Where("user_name =?", self.UserName)
	}

	if self.PassWord != "" {
		(*query).Where("pass_word =?", self.PassWord)
	}

	if self.LoginNum > 0 {
		(*query).Where("login_num =?", self.LoginNum)
	}

	if self.State > 0 {
		(*query).Where("state =?", self.State)
	}

	if self.OnlieState > 0 {
		(*query).Where("onlie_state =?", self.OnlieState)
	}

	if self.IsAdmin > 0 {
		(*query).Where("is_admin =?", self.IsAdmin)
	}

	if self.IpState > 0 {
		(*query).Where("ip_state =?", self.IpState)
	}

	if self.Ip != "" {
		(*query).Where("ip =?", self.Ip)
	}

	if !self.LastLoginTime.IsZero() {
		(*query).Where("last_login_time =?", self.LastLoginTime)
	}

	if self.LastIp != "" {
		(*query).Where("last_ip =?", self.LastIp)
	}

	if self.TenantType > 0 {
		(*query).Where("tenant_type =?", self.TenantType)
	}

	if self.TenantId > 0 {
		(*query).Where("tenant_id =?", self.TenantId)
	}

	if self.DelState > 0 {
		(*query).Where("del_state =?", self.DelState)
	}

	if self.AddUser != "" {
		(*query).Where("add_user =?", self.AddUser)
	}

	if self.AddUserId > 0 {
		(*query).Where("add_user_id =?", self.AddUserId)
	}

	if !self.AddTime.IsZero() {
		(*query).Where("add_time =?", self.AddTime)
	}

	if self.EditUser != "" {
		(*query).Where("edit_user =?", self.EditUser)
	}

	if self.EditUserId > 0 {
		(*query).Where("edit_user_id =?", self.EditUserId)
	}

	if !self.LastEditTime.IsZero() {
		(*query).Where("last_edit_time =?", self.LastEditTime)
	}

	if self.ServerNumber > 0 {
		(*query).Where("server_number =?", self.ServerNumber)
	}

	if !self.DelTime.IsZero() {
		(*query).Where("del_time =?", self.DelTime)
	}

}
func (self *SysAccountDto) BuildEntityForAdd(entity *model.SysAccount) {

	if self.Id > 0 {
		entity.Id = self.Id
	} else {
		entity.Id = 0
	}

	if self.UserName != "" {
		entity.UserName = self.UserName
	} else {
		entity.UserName = ""
	}

	if self.PassWord != "" {
		entity.PassWord = self.PassWord
	} else {
		entity.PassWord = ""
	}

	if self.LoginNum > 0 {
		entity.LoginNum = self.LoginNum
	} else {
		entity.LoginNum = 0
	}

	if self.State > 0 {
		entity.State = self.State
	} else {
		entity.State = 0
	}

	if self.OnlieState > 0 {
		entity.OnlieState = self.OnlieState
	} else {
		entity.OnlieState = 0
	}

	if self.IsAdmin > 0 {
		entity.IsAdmin = self.IsAdmin
	} else {
		entity.IsAdmin = 0
	}

	if self.IpState > 0 {
		entity.IpState = self.IpState
	} else {
		entity.IpState = 0
	}

	if self.Ip != "" {
		entity.Ip = self.Ip
	} else {
		entity.Ip = ""
	}

	if !self.LastLoginTime.IsZero() {
		entity.LastLoginTime = self.LastLoginTime
	} else {
		entity.LastLoginTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.LastIp != "" {
		entity.LastIp = self.LastIp
	} else {
		entity.LastIp = ""
	}

	if self.TenantType > 0 {
		entity.TenantType = self.TenantType
	} else {
		entity.TenantType = 0
	}

	if self.TenantId > 0 {
		entity.TenantId = self.TenantId
	} else {
		entity.TenantId = 0
	}

	if self.DelState > 0 {
		entity.DelState = self.DelState
	} else {
		entity.DelState = 0
	}

	if self.AddUser != "" {
		entity.AddUser = self.AddUser
	} else {
		entity.AddUser = ""
	}

	if self.AddUserId > 0 {
		entity.AddUserId = self.AddUserId
	} else {
		entity.AddUserId = 0
	}

	if !self.AddTime.IsZero() {
		entity.AddTime = self.AddTime
	} else {
		entity.AddTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.EditUser != "" {
		entity.EditUser = self.EditUser
	} else {
		entity.EditUser = ""
	}

	if self.EditUserId > 0 {
		entity.EditUserId = self.EditUserId
	} else {
		entity.EditUserId = 0
	}

	if !self.LastEditTime.IsZero() {
		entity.LastEditTime = self.LastEditTime
	} else {
		entity.LastEditTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.ServerNumber > 0 {
		entity.ServerNumber = self.ServerNumber
	} else {
		entity.ServerNumber = 0
	}

}
func (self *SysAccountDto) BuildEntityForUpdate(entity *model.SysAccount) {

	entity.Id = self.Id

	if self.Id > 0 {
		entity.Id = self.Id
	}

	if self.UserName != "" {
		entity.UserName = self.UserName
	}

	if self.PassWord != "" {
		entity.PassWord = self.PassWord
	}

	if self.LoginNum > 0 {
		entity.LoginNum = self.LoginNum
	}

	if self.State > 0 {
		entity.State = self.State
	}

	if self.OnlieState > 0 {
		entity.OnlieState = self.OnlieState
	}

	if self.IsAdmin > 0 {
		entity.IsAdmin = self.IsAdmin
	}

	if self.IpState > 0 {
		entity.IpState = self.IpState
	}

	if self.Ip != "" {
		entity.Ip = self.Ip
	}

	if !self.LastLoginTime.IsZero() {
		entity.LastLoginTime = self.LastLoginTime
	} else {
		entity.LastLoginTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.LastIp != "" {
		entity.LastIp = self.LastIp
	}

	if self.TenantType > 0 {
		entity.TenantType = self.TenantType
	}

	if self.TenantId > 0 {
		entity.TenantId = self.TenantId
	}

	if self.DelState > 0 {
		entity.DelState = self.DelState
	}

	if self.AddUser != "" {
		entity.AddUser = self.AddUser
	}

	if self.AddUserId > 0 {
		entity.AddUserId = self.AddUserId
	}

	if !self.AddTime.IsZero() {
		entity.AddTime = self.AddTime
	} else {
		entity.AddTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.EditUser != "" {
		entity.EditUser = self.EditUser
	}

	if self.EditUserId > 0 {
		entity.EditUserId = self.EditUserId
	}

	if !self.LastEditTime.IsZero() {
		entity.LastEditTime = self.LastEditTime
	} else {
		entity.LastEditTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.ServerNumber > 0 {
		entity.ServerNumber = self.ServerNumber
	}

}
